package commands;

import utils.CommandStack;
import utils.EditorContext;
import utils.FileManager;

public class LoadCommand extends Command {
    private String filePath;

    public LoadCommand(EditorContext context, String path) {
        super(context);
        if (!FileManager.isValidPath(path)) {
            throw new IllegalArgumentException("The provided path contains illegal characters.");
        }
        this.filePath = path;
    }

    @Override
    public void execute() {
        String loadedContent = FileManager.loadFile(filePath);
        context.setContent(loadedContent);
        context.setCurrentFilePath(filePath);
        context.setState(EditorContext.EditorState.EDITING);
        CommandStack.getInstance().add(new LoadCommand(
                new EditorContext(new StringBuilder(context.getContent())), filePath));
    }
}
